const express = require('express');
const fs = require('fs');
const app = express();
const time=require('time-stamp');
const url = require('url')
const path = require('path');
app.use('/static', express.static(path.join(__dirname, 'static')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.listen(3000);
// 登录页面
app.get('/login', (req, res) => {
    res.sendFile(path.join(__dirname, 'login.html'));
})
app.post('/dologin', (req, res) => {
    // console.log(req.body);
    // url模块
    let { query: { username, password } } = url.parse(req.url, true);
    let data = JSON.parse(fs.readFileSync(path.join(__dirname, 'persons.json')));
    // console.log(data);
    let islogin = data.find(value => value.tel == username && value.pass == password);
    if (islogin) {
        res.send('登陆成功')
    } else {
        res.send('用户名或密码输入错误')
    }
})
// 注册页面
app.get('/register', (req, res) => {
    res.sendFile(path.join(__dirname, 'register.html'));
})
app.use((req,res,next)=>{
    req.hh=time('YYYY年MM月DD日');
    next();
})
app.post('/doregister',(req,res)=>{
    

    let { query :{username,password,time}}=url.parse(req.url,true);
    let data=JSON.parse(fs.readFileSync(path.join(__dirname,'persons.json')));
    let isregister=data.find(value=>value.tel==username);
    if(isregister){
        res.send('已注册')
    }else{
        data.push({tel,pass});
        fs.writeFileSync(path.join(__dirname,'persons.json'),JSON.stringify(data));
        res.send('注册成功')
    }
})
